#ifndef _TARGETBL_INTERFACE_H_
#define _TARGETBL_INTERFACE_H_

#include "TargetBL_Config.h"
#include "flash_if.h"
// Timer0连续计时, 每us的计数值
/*#define US_TO_TICK(us)    ((U32)((us) * (SYSCLK/1000000.0) / 12))    // us => tick
#define ONE_MS            ((U16)US_TO_TICK(1000))                    // 1ms
#define TEN_MS            ((U16)US_TO_TICK(10000))                // 10ms
#define FIVE_SEC        ((U16)(US_TO_TICK(5000000UL)/65536))    // 5s*/

#define HCLK                160000000
#define APB1_PRESCAL        4
#define TIM7_SYSCLK            (HCLK * 2 / APB1_PRESCAL)
#define TIM7_PRESCAL        80

#define US_TO_TICK_UL(us)                    ((ulong)((us) * 1e-6 * TIM7_SYSCLK / TIM7_PRESCAL))    // 将us时间转换为定时器计数值, 最大65.536ms
#define US_TO_TICK(us1)                       (us1)
#define ONE_MS                                US_TO_TICK(1000)        // 1ms
#define TWO_MS                                US_TO_TICK(2000)        // 1ms
#define FIVE_MS                                US_TO_TICK(5000)        // 10ms
#define TEN_MS                                US_TO_TICK(10000)        // 10ms
#define TWENTY_MS                            US_TO_TICK(20000)        // 10ms
#define FIVE_SEC                            ((U16)(US_TO_TICK(5000000UL)/65536))    // 5s
#define TICK_TO_US(tick)                    ((ulong)((tick)*(TIM7_PRESCAL * 1e6 / TIM7_SYSCLK)))


//******************************************************************************************
/* Define the address from where user application will be loaded.
   Note: the 1st sector 0x08000000-0x08007FFF is reserved for the IAP code */

#define TGT_SIZE    0x0C

#define TGT_APP_INFOBLOCK_ADDRESS         (USER_FLASH_END_ADDRESS - TGT_SIZE)

#define FLASH_SIG_ADDRESS                 (TGT_APP_INFOBLOCK_ADDRESS + TGT_SIZE -  SIG_SIZE + 1)

#define FLASH_DEVICE_CODE_ADDRESS         (TGT_APP_INFOBLOCK_ADDRESS + 1)

#define TGT_BOOT_INFOBLOCK_ADDRESS        (uint32_t)0x080fffec

//////////////////////////////////////////////


// ----------------------------------
// Target BL Config Options
// ----------------------------------
#define TGT_BL_LOCK_BYTE                (OB_WRP_SECTOR_0 | OB_WRP_SECTOR_1)
#define TGT_BL_FW_INFOBLOCK_LENGTH      20
#define TGT_BL_RESERVERD                0           // keep 4 bytes aligned


#define TGT_FLASH_PAGE_ADDR_MASK       (TGT_FLASH_PAGE_SIZE - 1)
#define TGT_BL_PKT_SIZE_MASK           (TGT_BL_PKT_SIZE - 1)
#define TGT_BL_BUF_SIZE_MASK           (TGT_BL_BUF_SIZE - 1)

#define TGT_FLASH_PAGE_SIZE            (1 << TGT_FLASH_PAGE_SIZE_CODE)              //flash页大小
#define APP_FW_END_ADDR                USER_FLASH_END_ADDRESS
#define TGT_BL_PKT_SIZE                (1 << TGT_BL_PKT_SIZE_CODE)
#define TGT_BL_BUF_SIZE                (1 << TGT_BL_BUF_SIZE_CODE)



#define APP_FW_SIG0_ADDR            (APP_FW_END_ADDR)
#define APP_FW_SIG3_ADDR            (APP_FW_END_ADDR - 3)

// ----------------------------------
// Signature Bytes
// ----------------------------------
#define SIG_BYTE0                0x3D
#define SIG_BYTE1                0xC2
#define SIG_BYTE2                0xA5
#define SIG_BYTE3                0x1B
#define SIG_BYTE                 0x3DC2A51B

//-----------------------------------------------------------------------------
// Device Code
//-----------------------------------------------------------------------------

#define DEVICE_CODE              0x10

//-----------------------------------------------------------------------------
// "Public" Global Variables - Externs - Available to all modules
//-----------------------------------------------------------------------------

extern U8 Flash_Key0;
extern U8 Flash_Key1;

//-----------------------------------------------------------------------------
// "Public" Function Prototypes - Externs - Available to all modules
//-----------------------------------------------------------------------------

// This is usually defined in the STARTUP.A51 file.
void START_APPLICATION(void);

#ifdef CODE_BANK
typedef U32 addr_t;

// This is usually defined in the L51_BANK.A51 file.
void switchbank(unsigned char bank_number);
#else// CODE_BANK
typedef U16 addr_t;
#endif// CODE_BANK

// ---------------------------------
// Device-Specific Functions
// Defined in "Fnnn_TargetBL_DevSpecific.c"
// ---------------------------------
void Device_Init(void);
//U16 SystemTick(void);
bit Timeouts(void);
void ResetTimeouts(void);

// ---------------------------------
// Flash Read/Erase/Write Functions
// Defined in "Fnnn_Flash.c"
// ---------------------------------
//void FLASH_Modify(addr_t addr, U8 byte);
//U8 FLASH_Read(addr_t addr);

// -------------------------------------
// Bootloader Command Processors
// Defined in "TargetBL_Commands.c"
// -------------------------------------
void TGT_Erase_Page(U32 Addr);
void TGT_Write_Flash(U8  *buf, U32 addr);

void Target_Init(void);
int Target_main(void);


#endif // #ifndef _TARGETBL_INTERFACE_H_
